#include common_scripts\utility;
#include maps\mp\_utility;
#include maps\mp\gametypes\_hud_util;
#include z_migliore\utility;


init()
{
    precacheShader("hint_health");
    
    level thread onPlayerConnected();
}

onPlayerConnected()
{
    for(;;) {
        level waittill("connected", player);
        
        
        player.zClass_Building = undefined;
        player.zClass_CanChange = true;
        player.zClass_Current = -1;
        player.zClass_Wanted = 0;
        player.zClass_PrimaryWeapon = "";
        player.zClass_SecondaryWeapon = "";
        
        player.zMain_AtBase = true;
        player.zMain_LastAttackOrigin = (0, 0, 0);
        player.zMain_LastAttackTime = getTime();
        
        player.zMenu_UsingClassMenu = false;
        player.zMenu_UsingWeaponMenu = false;
        player.zMenu_UsingWeaponMenuDelay = false;
        
        player.zTeleport_CanUse = true;
        
        player.zWeapon_Wanted = 0;
        
        
        player thread onPlayerDied();
        player thread onPlayerShot();
        player thread onPlayerSpawned();
        
        player thread Hud_Health();
        player thread Hud_Weapon();
        
        player notifyOnPlayerCommand("zkey_actionslot_2", "+actionslot 2"); // меню - открыть/закрыть
        player notifyOnPlayerCommand("zkey_activate", "+activate"); // меню - применить
        player notifyOnPlayerCommand("zkey_back", "+back"); // меню - вниз
        player notifyOnPlayerCommand("zkey_forward", "+forward"); // меню - вверх
        player notifyOnPlayerCommand("zkey_moveleft", "+moveleft"); // меню - влево
        player notifyOnPlayerCommand("zkey_moveright", "+moveright"); // меню - вправо
    }
}

onPlayerDied()
{
    self endon("disconnect");
    
    for(;;) {
        self waittill("death");
        // ...
    }
}

onPlayerShot()
{
    self endon("disconnect");
    
    for(;;) {
        self waittill("weapon_fired", weaponName);
        
        if(!isSubStr(weaponName, "silencer")) {
            self.zMain_LastAttackOrigin = self.origin;
            self.zMain_LastAttackTime = getTime();
        }
    }
}

onPlayerSpawned()
{
    self endon("disconnect");
    
    for(;;) {
        self waittill("spawned_player");
        
        self.zClass_CanChange = true;
        
        self.zMain_AtBase = true;
        
        self.zMenu_UsingClassMenu = false;
        self.zMenu_UsingWeaponMenu = false;
        self.zMenu_UsingWeaponMenuDelay = false;
        
        self.zTeleport_CanUse = true;
    }
}

Hud_Health()
{
    self endon("disconnect");
    
    self.zHud_Health = self createIcon("hint_health", 32, 32);
    self.zHud_Health.alpha = 0;
    self.zHud_Health.color = (1, 1, 1);
    self.zHud_Health.foreGround = true;
    self.zHud_Health.hideWhenInMenu = true;
    self.zHud_Health setPoint("BOTTOM LEFT", "BOTTOM LEFT", 5, -5);
    
    for(;;) {
        result = self waittill_any_return("spawned_player", "death");
        
        if(result == "spawned_player" && !self.zHud_Health.alpha) {
            self.zHud_Health.alpha = 1;
            
            self thread Hud_HealthUpdate(self.zHud_Health);
        } else if(result == "death" && self.zHud_Health.alpha) {
            self.zHud_Health.alpha = 0;
        }
    }
}

Hud_HealthUpdate(hud_elem)
{
    self endon("death");
    self endon("disconnect");
    
    for(;;) {
        hud_elem.color = (1.0, self.health/self.maxhealth, self.health/self.maxhealth);
        
        wait(0.25);
    }
}

Hud_Weapon()
{
    self endon("disconnect");
    
    self.zHud_Weapon = [];
    
    self.zHud_Weapon["div"] = self createFontString("hudbig", 1);
    self.zHud_Weapon["div"].alpha = 0;
    self.zHud_Weapon["div"].foreGround = true;
    self.zHud_Weapon["div"].hideWhenInMenu = true;
    self.zHud_Weapon["div"] setPoint("TOP RIGHT", "TOP RIGHT", -50, 25);
    self.zHud_Weapon["div"] setText("|");
    
    self.zHud_Weapon["clip"] = self createFontString("hudbig", 1);
    self.zHud_Weapon["clip"].alpha = 0;
    self.zHud_Weapon["clip"].foreGround = true;
    self.zHud_Weapon["clip"].hideWhenInMenu = true;
    self.zHud_Weapon["clip"] setPoint("", "", -15, 10);
    self.zHud_Weapon["clip"] setParent(self.zHud_Weapon["div"]);
    
    self.zHud_Weapon["mags"] = self createFontString("hudbig", 0.65);
    self.zHud_Weapon["mags"].alpha = 0;
    self.zHud_Weapon["mags"].foreGround = true;
    self.zHud_Weapon["mags"].hideWhenInMenu = true;
    self.zHud_Weapon["mags"] setPoint("", "", 10, 10);
    self.zHud_Weapon["mags"] setParent(self.zHud_Weapon["div"]);
    
    for(;;) {
        result = self waittill_any_return("spawned_player", "death");
        
        if(result == "spawned_player" && !self.zHud_Weapon["div"].alpha) {
            self.zHud_Weapon["div"].alpha = 1;
            self.zHud_Weapon["clip"].alpha = 1;
            self.zHud_Weapon["mags"].alpha = 1;
            
            self thread Hud_WeaponUpdate(self.zHud_Weapon);
        } else if(result == "death" && self.zHud_Weapon["div"].alpha) {
            self.zHud_Weapon["div"].alpha = 0;
            self.zHud_Weapon["clip"].alpha = 0;
            self.zHud_Weapon["mags"].alpha = 0;
        }
    }
}

Hud_WeaponUpdate(hud_array)
{
    self endon("death");
    self endon("disconnect");
    
    for(;;) {
        self waittill("zhud_wpn_upd", clip, mags);
        
        hud_array["clip"] setValue(clip);
        hud_array["mags"] setValue(mags);
    }
}

